package com.nf.health.app.datamain.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.adapter.OnMyClickListener;
import com.nf.health.app.adapter.TemperatureHistoryDataAdapter;
import com.nf.health.app.customview.DragListView;
import com.nf.health.app.models.TempertuerHistoryModel;
import com.nf.health.app.utils.ViewUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 显示温度的历史数据记录
 */
public class TemperatureHistoryActivity extends BaseActivity {
    private final String REQUEST_TEMPATERDATAFLAG = "request.tempatature_data";
    private final String DELETE_DATA = "delete";
    @Bind(R.id.titlebar_back_view)
    ImageView titlebar_back_view;
    @Bind(R.id.lv_temperature_history)
    DragListView lv_temperature_history;
    /*************当前页数*****/
    private int currentPageNo = 0;
    /*************页数的大小*****/
    private int pageSize = 20;
    private List<TempertuerHistoryModel> list;
    private TemperatureHistoryDataAdapter adapter;
    private int delPosiyion;//删除的索引值
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_temperature_history);
        ButterKnife.bind(this);
        ViewUtil.setText(this, R.id.titlebar_title_tv, "历史记录");
        initListener();
    }
    private void initListener(){
    titlebar_back_view.setOnClickListener(this);
    onRefreshData();
        list = new ArrayList<TempertuerHistoryModel>();
        adapter = new TemperatureHistoryDataAdapter(TemperatureHistoryActivity.this,list);
        lv_temperature_history.setAdapter(adapter);

        lv_temperature_history.setRefreshableAndLoadMoreable(true, false);
        lv_temperature_history.setOnRefreshAndLoadMoreListener(new DragListView.onRefreshAndLoadMoreListener() {
        @Override
        public void onRefresh() {
            currentPageNo = 1;
            onRefreshData();
        }

        @Override
        public void onLoadMore() {
            loadMoewData();
        }
    });
        lv_temperature_history.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                adapter.closeAllOpenedSwipeView();
                adapter.setMyNotifyDatachanged(list);
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
}
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.btn_commit:
                break;
        }
    }
    /********加载更多数据************/
    private void onRefreshData(){
        currentPageNo =1;
        provider.requestTemperatureData("list",String.valueOf(currentPageNo), String.valueOf(pageSize), REQUEST_TEMPATERDATAFLAG);
    }
    /********下拉刷新************/
    private void loadMoewData(){
        currentPageNo ++;
        provider.requestTemperatureData("list",String.valueOf(currentPageNo), String.valueOf(pageSize), REQUEST_TEMPATERDATAFLAG);
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        hideLoading();
        if(actionName.equals(REQUEST_TEMPATERDATAFLAG)){

                try {
               JSONObject objs = new  JSONObject(object.toString());
                JSONArray  bojData = objs.optJSONArray("deviceData");
                list.clear();
                if(bojData != null){
                    int length = bojData.length();
                    for(int i = 0 ; i < length; i++){
                        String response = bojData.getString(i);
                        TempertuerHistoryModel subMOdel = new TempertuerHistoryModel().parseTempertuerHistoryModel(response);
                        list.add(subMOdel);
                    }

                }
                if(adapter == null){
                    adapter = new TemperatureHistoryDataAdapter(TemperatureHistoryActivity.this,list);
                    lv_temperature_history.setAdapter(adapter);
                }
                adapter.setMyNotifyDatachanged(list);
                    adapter.setOnMyClickListener(new OnMyClickListener() {
                        //点击删除对应id的历史数据
                        @Override
                        public void onClick(int position, String flag) {
                            String id = list.get(position).getId();
                            delPosiyion = position;
                            provider.delTemperatureData("temperature",id, DELETE_DATA);
                            adapter.closeAllOpenedSwipeView();
                            adapter.setMyNotifyDatachanged(list);
                        }
                    });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }else if(actionName.equals(DELETE_DATA)){
            list.remove(delPosiyion);
            adapter.setMyNotifyDatachanged(list);//更新数据
        }
        lv_temperature_history.completeLoadMore();
        lv_temperature_history.completeRefresh();

    }
    @Override
    public void handleActionStart(String actionName, Object object) {
        showLoading();
    }

    @Override
    public void handleActionFinish(String actionName, Object object) {
        hideLoading();
    }
}
